package com.lg.okframe.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lg.okframe.R;

/**
 * Created by LG on 2018/8/15.
 */

public class menu extends RelativeLayout {
    Context context;
    private yuan yuan332z;
    private int widths;
    private int heights;
    private boolean aBoolean;

    public menu(Context context) {
        super(context);
    }

    public menu(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inits(attrs);
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams centerButtonParams12 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0F);
        linearLayout.setLayoutParams(centerButtonParams12);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        centerButtonParams12.gravity = Gravity.CENTER_VERTICAL;

        LinearLayout.LayoutParams centerButtonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        for (int i = 0; i < 5; i++) {
            menusing menusing = new menusing(context);
            linearLayout.addView(menusing, centerButtonParams);
        }

        RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        relativeLayout.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(linearLayout, centerButtonParams12);

        RelativeLayout.LayoutParams relativeLayoutx = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeLayoutx.addRule(RelativeLayout.CENTER_IN_PARENT);
        yuan yuanx = new yuan(context);
        addView(yuanx, relativeLayoutx);

    }

    public menu(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        inits(attrs);
    }

    private void inits(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.yuan);
        aBoolean = typedArray.getBoolean(R.styleable.yuan_isAnmin, false);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        widths = MeasureSpec.getSize(widthMeasureSpec) / 2;
        heights = MeasureSpec.getSize(heightMeasureSpec) / 2;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View childAt = getChildAt(0);
        yuan332z = (yuan) getChildAt(1);

        if (childAt instanceof LinearLayout) {
            final LinearLayout ll = (LinearLayout) childAt;
            for (int i = 0; i < ll.getChildCount(); i++) {
                View childAt1 = ll.getChildAt(i);
                if (childAt1 instanceof menusing) {
                    final menusing childAt11 = (menusing) childAt1;
                    final int finalI = i;
                    childAt11.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (aBoolean) {
                                startAmin((menusing) ll.getChildAt(finalI));
                            } else {
                                Toast.makeText(context, "没开启哦！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        }

    }


    public void startAmin(menusing mIvImg) {
        /**
         * TranslateAnimation第一种构造
         *
         * @param fromXDelta X方向开始的位置，取值类型是float，单位是px像素
         * @param toXDelta X方向结束的位置，取值类型是float，单位是px像素
         * @param fromYDelta Y方向开始的位置，同上
         * @param toYDelta Y方向结束的位置，同上
         */

        int[] locationqq = new int[2];
        yuan332z.getLocationInWindow(locationqq);


        float y = mIvImg.getTop() + mIvImg.getY();
        float x = mIvImg.getLeft() + mIvImg.getX();

        float y1 = yuan332z.getTop() + yuan332z.getY();
        float x1 = yuan332z.getLeft() + yuan332z.getX();


        TranslateAnimation translateAnimation1 = new TranslateAnimation(0, widths, 0, heights - mIvImg.getTop());

/*
        TranslateAnimation translateAnimation1 = new TranslateAnimation(TranslateAnimation.ABSOLUTE, mIvImg.getWidth(), TranslateAnimation.ABSOLUTE, mIvImg
                .getWidth() * 2f, TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0);
*/
        //设置动画持续时长
        translateAnimation1.setDuration(1500);
        //设置动画结束之后的状态是否是动画的最终状态，true，表示是保持动画结束时的最终状态
        translateAnimation1.setFillAfter(true);
        //设置动画结束之后的状态是否是动画开始时的状态，true，表示是保持动画开始时的状态
        translateAnimation1.setFillBefore(false);
        translateAnimation1.setInterpolator(new BounceInterpolator());
        //设置动画的重复模式：反转REVERSE和重新开始RESTART
        translateAnimation1.setRepeatMode(ScaleAnimation.RELATIVE_TO_PARENT);
        //设置动画播放次数
        translateAnimation1.setRepeatCount(ScaleAnimation.RELATIVE_TO_PARENT);
        //开始动画
        mIvImg.startAnimation(translateAnimation1);
/*        //清除动画
        mIvImg.clearAnimation();
        //同样cancel（）也能取消掉动画
        translateAnimation1.cancel();*/


        translateAnimation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                yuan332z.startAnmin();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    private float mFactor = 1.0f;

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
